package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.asktgapp.MyConstant;
import com.asktgapp.R;
import com.asktgapp.base.BaseFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 新我的关注页面
 */

public class MyPublishNewFragment extends BaseFragment {
    @InjectView(R.id.tablayout)
    TabLayout mTabLayout;
    @InjectView(R.id.viewpager)
    ViewPager mViewPager;
    private static final int PAGE_COUNT = 4;

    private TabAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_my_publish, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mAdapter = new TabAdapter(getFragmentManager());
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);
        int index  = getBaseActivity().getIntent().getIntExtra("type",0);
        mViewPager.setCurrentItem(index);
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    public class TabAdapter extends FragmentPagerAdapter {

        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            int type;
            switch (position) {
                case 0:
                    type = MyConstant.TYPE_FREE_ASK;//问答
                    break;
                case 1:
                    type = MyConstant.TYPE_USED;//二手设备
                    break;
                case 2:
                    type = MyConstant.TYPE_RENT;//设备出租
                    break;
                case 3:
                    type = MyConstant.TYPE_HOME_SERVICE;//上门服务
                    break;
                default:
                    type = MyConstant.TYPE_FREE_ASK;
                    break;
            }
            return MyPublishItemNewFragment.newInstance(type);
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "快速问答";
                case 1:
                    return getString(R.string.homePage_used_machine);
                case 2:
                    return "设备出租";
                case 3:
                    return "上门服务";
                default:
                    return "快速问答";
            }
        }
    }
}
